package net.androy.mc;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class Main extends MapActivity {
    private MapView myMapView;
    private int x;
    private int y;

    @Override
    public void onCreate(Bundle icicle) {
	super.onCreate(icicle);
	myMapView = new MapView(this, "0R0OwUVMvskBWuBAVrTQEqbxBbOvEGITLNKwpBQ");
	myMapView.setClickable(true);
	myMapView.setEnabled(true);
	// Lets start at the Statue of Liberty
	// I grabbed the data from Google-Maps
	final GeoPoint p = new GeoPoint((int) (40.689213 * 1000000), (int) (-74.044558 * 1000000));
	// Get the controller, that is used for translation and zooming
	MapController mc = myMapView.getController();
	// Translate to the Statue of Liberty
	mc.animateTo(p);

	// Zoom Very close
	mc.setZoom(16);
	// Enable Sattelite-Mode, so we will se the
	// Statue of liberty instantly on the screen
	myMapView.setSatellite(true);

	View zoomView = myMapView.getZoomControls();
	zoomView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
		ViewGroup.LayoutParams.WRAP_CONTENT));
	myMapView.addView(zoomView);
	myMapView.displayZoomControls(true);

	// Bitmap b = BitmapFactory.decodeResource(getResources(),
	// R.drawable.info);
	Drawable d = getResources().getDrawable(R.drawable.info);
	myMapView.getOverlays().add(new PersonMarker(d, p));

	// Make myMapView the exilicit view of this app
	setContentView(myMapView);

    }

    // public boolean dispatchTouchEvent(MotionEvent ev) {
    // int action = ev.getAction();
    //
    // switch (action) {
    // case MotionEvent.ACTION_DOWN: {
    // x = (int) ev.getX();
    // y = (int) ev.getY();
    //        
    // Log.i("LOCX::", x + "");
    // Log.i("LOCY::", y + "");
    // break;
    // }
    //        
    // case MotionEvent.ACTION_UP: {
    // int x2 = (int) ev.getX();
    // int y2 = (int) ev.getY();
    // GeoPoint newPoint = myMapView.getProjection().fromPixels(Math.abs(x2-x),
    // Math.abs(y2 - y));
    // myMapView.getController().animateTo(newPoint);
    // break;
    // }
    // }
    //
    // return false;
    // }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_I) {
	    // Zooming In
	    myMapView.getController().zoomIn();
	    return true;
	} else if (keyCode == KeyEvent.KEYCODE_O) {
	    // Zooming Out

	    myMapView.getController().zoomOut();
	    return true;
	} else if (keyCode == KeyEvent.KEYCODE_S) {
	    // Switch to satellite view
	    myMapView.setSatellite(true);
	    ;
	    return true;
	} else if (keyCode == KeyEvent.KEYCODE_T) {
	    // Switch on traffic overlays
	    myMapView.setTraffic(true);
	    return true;
	}
	return false;
    }

    @Override
    protected boolean isRouteDisplayed() {
	// TODO Auto-generated method stub
	return false;
    }

    class PersonMarker extends ItemizedOverlay<OverlayItem> {
	private List<OverlayItem> items = new ArrayList<OverlayItem>();
	private Drawable marker = null;

	public PersonMarker(Drawable marker, GeoPoint p) {
	    super(marker);
	    this.marker = marker;

	    items.add(new OverlayItem(p, "UN", "United Nations"));
	    populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
	    return (items.get(i));
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
	    super.draw(canvas, mapView, shadow);

	    boundCenterBottom(marker);
	}

	@Override
	protected boolean onTap(int i) {
	    Toast.makeText(Main.this, items.get(i).getSnippet(), Toast.LENGTH_SHORT).show();

	    return (true);
	}

	@Override
	public int size() {
	    return (items.size());
	}
    }
}